/**
 * @file ngmessage.cpp
 * @author Suk Robert Koh (srkoh87z@gmail.com)
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ngine/common/message/ngmessage.h"

/**
 * Default constructor for NGMessage class
 */
NGMessage::NGMessage()
{
	// initialize variables
	type = 0;
	length = 0;
	userId = 0;
	data = 0;
}

/**
 * Destructor for NGMessage class
 */
NGMessage::~NGMessage()
{
	if(data == NULL)
		free(data);
}

/**
 * Gets the type of the message
 *
 * @return	BYTE	Type of the message
 */
BYTE NGMessage::getType()
{
	return type;
}

/**
 * Gets the length of the message
 *
 * @return	WORD	Length of the message
 */
WORD NGMessage::getLength()
{
	return length;
}

/**
 * Gets the sender's id
 *
 * @return	DWORD	Sender's id
 */
DWORD NGMessage::getSenderId()
{
	return userId;
}

/**
 * Gets the actual data of the message
 *
 * @return char *	Pointer to the data of the message
 */
char *NGMessage::getData()
{
	return data;
}

/**
 * Sets the type of the message
 *
 * @param	BYTE	Type of the message
 */
void NGMessage::setType(BYTE _type)
{
	type = _type;
}

/**
 * Sets the sender's id
 *
 * @param	DWORD	Sender's id
 */
void NGMessage::setSenderId(DWORD _userId)
{
	userId = _userId;
}

/**
 * Sets the data of the message
 *
 * @param	const char *		Pointer to the message of the data
 */
#include <stdio.h>
void NGMessage::setData(const char *_data)
{
	// exit out if received param data is null
	if(_data == NULL)
		return;

	// free existing data if it is not null
	if(data != NULL)
		free(data);

	// sets the length of the data
	length = strlen(_data);

	// mallocs a space
	data = (char *) malloc(length + 1);
	if(data != NULL)
	{
		/*
		 * TODO: handle malloc
		 */
		memcpy(data, _data, length + 1);
	}
}



